package redisclient

import "gitee.com/lsy007/redisclient/model"

// Type(key string)(string,err error)
// PExpire(key string, second int64)(bool,err error)
// PExpireAt(key string, timpstamp int64)(bool,err error)
// Rename(key string, newKey string)(string,err error)
// Persist(key string)(bool,err error)
// Move(key string, db int64)(bool,err error)
// RandomKey()(string,err error)
// Dump(key string)(value []byte, err error)
// Restore(key string, ttl int64, value []byte) (status string, err error)
// TTL(key string) (float64,err error)
// Expire(key string, second int64)(bool,err error)
// Del(keys ...string)(int64,err error)
// PTTL(key string) (float64,err error)
// RenameNX(key string, newKey string)(bool,err error)
// Exists(keys ...string)(int64,err error)
// ExpireAt(key string, timestamp int64)(bool,err error)
// Keys(pattern string)([]string,err error)

// 返回 key 所储存的值的类型
// 如果 key 不存在，则返回 none
func (c *RedisClient) Type(key string) (valueType string, err error) {
	args := &model.RedisArgsModel{Key: key}
	response, err = c.Request("Type", "Key", args)
	return response.String, err
}

// 设置 key 在 millisecond 毫秒后过期，key 过期后将不再可用
func (c *RedisClient) PExpire(key string, millisecond int64) (status bool, err error) {
	args := &model.RedisArgsModel{Key: key, Time: millisecond}
	response, err = c.Request("PExpire", "Key", args)
	return response.Bool, err
}

// 以 UNIX 时间戳设置 key 在指定具体时间过期，key 过期后将不再可用
func (c *RedisClient) PExpireAt(key string, timestamp int64) (status bool, err error) {
	args := &model.RedisArgsModel{Key: key, Unix: timestamp}
	response, err = c.Request("PExpireAt", "Key", args)
	return response.Bool, err
}

// 修改 key 的名称，成功返回 OK ，失败时候返回一个错误
func (c *RedisClient) Rename(key string, newKey string) (status string, err error) {
	args := &model.RedisArgsModel{Key: key, NewKey: newKey}
	response, err = c.Request("Rename", "Key", args)
	return response.String, err
}

// 移除给定 key 的过期时间，使得 key 永不过期
func (c *RedisClient) Persist(key string) (status bool, err error) {
	args := &model.RedisArgsModel{Key: key}
	response, err = c.Request("Persist", "Key", args)
	return response.Bool, err
}

// 将当前数据库的 key 移动到给定的数据库 db 当中
func (c *RedisClient) Move(key string, db int64) (status bool, err error) {
	args := &model.RedisArgsModel{Key: key, Db: db}
	response, err = c.Request("Move", "Key", args)
	return response.Bool, err
}

// 从当前数据库中随机返回一个 key
func (c *RedisClient) RandomKey() (key string, err error) {
	args := &model.RedisArgsModel{}
	response, err = c.Request("RandomKey", "Key", args)
	return response.String, err
}

// 序列化给定 key ，并返回被序列化的值
func (c *RedisClient) Dump(key string) (value []byte, err error) {
	args := &model.RedisArgsModel{Key: key}
	response, err = c.Request("Dump", "Key", args)
	return response.Bytes, err
}

// 反序列化给定的序列化值，并将它和给定的 key 关联，以毫秒为单位为 key 设置生存时间，如果 ttl 为 0 ，那么不设置生存时间
func (c *RedisClient) Restore(key string, ttl int64, value []byte) (status string, err error) {
	args := &model.RedisArgsModel{Key: key, Time: ttl, DumpBytes: value}
	response, err = c.Request("Restore", "Key", args)
	return response.String, err
}

// 以秒为单位返回 key 的剩余过期时间
func (c *RedisClient) TTL(key string) (time float64, err error) {
	args := &model.RedisArgsModel{Key: key}
	response, err = c.Request("TTL", "Key", args)
	return response.Float, err
}

// 设置 key 在 second 秒后过期，key 过期后将不再可用
func (c *RedisClient) Expire(key string, second int64) (status bool, err error) {
	args := &model.RedisArgsModel{Key: key, Time: second}
	response, err = c.Request("Expire", "Key", args)
	return response.Bool, err
}

// 用于删除已存在的键，不存在的 key 会被忽略
func (c *RedisClient) Del(keys ...string) (count int64, err error) {
	args := &model.RedisArgsModel{Keys: keys}
	response, err = c.Request("Del", "Key", args)
	return response.Int, err
}

// 以毫秒为单位返回 key 的剩余过期时间
func (c *RedisClient) PTTL(key string) (time float64, err error) {
	args := &model.RedisArgsModel{Key: key}
	response, err = c.Request("PTTL", "Key", args)
	return response.Float, err
}

// 在新的 key 不存在时修改指定 key 的名称
func (c *RedisClient) RenameNX(key string, newKey string) (status bool, err error) {
	args := &model.RedisArgsModel{Key: key, NewKey: newKey}
	response, err = c.Request("RenameNX", "Key", args)
	return response.Bool, err
}

// 检查给定的多个 key 是否存在，返回存在的数量
func (c *RedisClient) Exists(keys ...string) (count int64, err error) {
	args := &model.RedisArgsModel{Keys: keys}
	response, err = c.Request("Exists", "Key", args)
	return response.Int, err
}

// 以指定 UNIX 时间戳格式设置 key 的过期时间，key 过期后将不再可用
func (c *RedisClient) ExpireAt(key string, timestamp int64) (status bool, err error) {
	args := &model.RedisArgsModel{Key: key, Time: timestamp}
	response, err = c.Request("ExpireAt", "Key", args)
	return response.Bool, err
}

// 用于查找所有符合给定模式 pattern 的 key
func (c *RedisClient) Keys(pattern string) (keys []string, err error) {
	args := &model.RedisArgsModel{Pattern: pattern}
	response, err = c.Request("Keys", "Key", args)
	return response.StringSlice, err
}

// 迭代获取 Redis 键值，找出满足特定前缀 key 的列表
func (c *RedisClient) Scan(cursor int64, match string, count int64) (keys []string, nextCursor int64, err error) {
	args := &model.RedisArgsModel{Cursor: cursor, Match: match, Count: count}
	response, err = c.Request("Scan", "Key", args)
	return response.StringSlice, response.Int, err
}
